﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Windows.Infrastructure.Share.Tools;

namespace Windows.Basics.Application
{
    public class EnumService : IEnumService
    {
        /// <summary>
        /// 获取枚举类型下拉框列表
        /// </summary>
        /// <returns></returns>
        public List<ComboBoxResponse> GetComboBoxs(string code)
        {
            string assemblyFile = AppContext.BaseDirectory + "Windows.SeedWord.dll";
            Assembly assembly = Assembly.LoadFrom(assemblyFile);
            List<KeyValuePair<int, string>> enums =
                new List<KeyValuePair<int, string>>();

            Type[] types = assembly.GetTypes().Where(x=>x.BaseType?.FullName == typeof(Enum).FullName).ToArray();
            Type type = types.Where(x => x.Name.ToLower().Contains(code.ToLower())).FirstOrDefault();
            
            foreach (var item in Enum.GetValues(type))
            {
                var attrs = item.GetType().GetField(item.ToString())
                    .GetCustomAttributes(typeof(DescriptionAttribute), true);
                string desc = string.Empty;
                if (attrs != null && attrs.Length > 0)
                {
                    DescriptionAttribute descAttr = attrs[0] as DescriptionAttribute;
                    desc = descAttr.Description;
                }
                KeyValuePair<int, string> keyValuePair =
                    new KeyValuePair<int, string>(Convert.ToInt32(item), desc);
                enums.Add(keyValuePair);
            }
            List<ComboBoxResponse> list = new();
            //if (isShowDefalut)
            //{
            //    list.Add(new ComboBoxResponse()
            //    {
            //        Value = "",
            //        Name = "全部"
            //    });
            //}
            foreach (var d in enums)
            {
                ComboBoxResponse model = new ComboBoxResponse();
                model.Value = d.Key;
                model.Name = d.Value;
                list.Add(model);
            }
            return list;
        }
    }
}
